package MapReduce.Demo6_PhoneTrafficSort;

import MapReduce.writableBean.PhoneTrafficWritable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

/**
 * @Author lixinlei
 * @Date 2023/3/20 10:41
 */
public class PhoneTrafficSortComparator extends WritableComparator {

    /**
     * 通过自定义排序类的构造方法指定 需要排序的类型 和 是否创建比较实例
     */
    public PhoneTrafficSortComparator(){
        super(PhoneTrafficWritable.class,true);
    }

    @Override
    public int compare(WritableComparable a, WritableComparable b) {

        PhoneTrafficWritable aBean = (PhoneTrafficWritable)a;
        PhoneTrafficWritable bBean = (PhoneTrafficWritable)b;

        int thisSumFlow = aBean.getSumFlow();
        int thatSumFlow = bBean.getSumFlow();

        int thisDownFlow = aBean.getDownFlow();
        int thatDownFlow = bBean.getDownFlow();


        if(thatSumFlow == thisSumFlow){
            return thatDownFlow-thisDownFlow;
        }else{
            return thisSumFlow-thatSumFlow;
        }

    }
}
